package com.midea.iot.privateLessons.disruptor; 

import com.lmax.disruptor.RingBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description 生产
 * @auther xingwuxw
 * @create 2022-05-02
 */
public class MessageEventProducer<T> {

    private static final Logger logger = LoggerFactory.getLogger(MessageEventProducer.class);

    private RingBuffer<MessageEvent<T>> ringBuffer;

    public MessageEventProducer(RingBuffer<MessageEvent<T>> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public RingBuffer<MessageEvent<T>> getRingBuffer() {
        return ringBuffer;
    }

    public void setRingBuffer(RingBuffer<MessageEvent<T>> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void push(T data) {
        long seq = ringBuffer.next();
        try {
            MessageEvent<T> t = ringBuffer.get(seq);
            t.setData(data);
        } finally {
            ringBuffer.publish(seq);
        }
    }
}